using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using LevelData;

namespace GGJ11
{
    class SoundManager
    {
        public bool isSoundActive { get; set; }
        private SoundEffectInstance jump;
        private SoundEffectInstance battlePlayer;
        private SoundEffectInstance battleEnemy;
        private SoundEffectInstance dieEnemy;
        private SoundEffectInstance diePlayer;        
        private SoundEffectInstance music;
        private SoundEffectInstance story;
        private SoundEffectInstance openDoor;
        private SoundEffectInstance door;
        private SoundEffectInstance nextLevel;
        private SoundEffect poison;
        private SoundEffect dollar;
        private SoundEffect invulnerability;

        private Level level;
        private Random rand;

        private AudioListener cameraListener;

        public SoundManager(ContentManager content)
        {

            rand = new Random();
            level = content.ServiceProvider.GetService(typeof(Level)) as Level;

            jump = content.Load<SoundEffect>("Sounds/PlayerJump").CreateInstance();
            battlePlayer = content.Load<SoundEffect>("Sounds/attack").CreateInstance();
            battleEnemy = content.Load<SoundEffect>("Sounds/PlayerFall").CreateInstance();
            diePlayer = content.Load<SoundEffect>("Sounds/PlayerKilled").CreateInstance();
            dieEnemy = content.Load<SoundEffect>("Sounds/MonsterKilled").CreateInstance();
            story = content.Load<SoundEffect>("Sounds/songStory0").CreateInstance();// + level.numLevel.ToString()).CreateInstance();
            music = content.Load<SoundEffect>("Sounds/songGame0").CreateInstance();//+level.numLevel.ToString()).CreateInstance();
            openDoor = content.Load<SoundEffect>("Sounds/openDoor").CreateInstance();
            door = (content.Load<SoundEffect>("Sounds/door")).CreateInstance();
            nextLevel = content.Load<SoundEffect>("Sounds/ExitReached").CreateInstance();
            poison = content.Load<SoundEffect>("Sounds/poison");
            dollar = content.Load<SoundEffect>("Sounds/GemCollected");
            invulnerability = content.Load<SoundEffect>("Sounds/PowerUp");

            level.playerLevel.timeInvulnerability = (float)invulnerability.Duration.TotalSeconds;

            battlePlayer.Volume = 0.5f;
            battleEnemy.Volume = 0.5f;
            openDoor.Volume = 0.5f;
            door.Volume = 0.5f;
            diePlayer.Volume = 0.5f;
            dieEnemy.Volume = 0.5f;
            
            cameraListener = new AudioListener();
            cameraListener.Position =
                new Vector3(
                    GraphicsDeviceManager.DefaultBackBufferWidth / 2,
                    GraphicsDeviceManager.DefaultBackBufferHeight / 2,
                    0);

            music.IsLooped = true;
            music.Play();
            isSoundActive = true;
        }

        public void updateSounds()
        {
            if (!isSoundActive)
                return;

            for (int i = 0; i < level.soundList.Count; i++)
            {
                switch (level.soundList[i])
                { 
                    case SoundType.jump:
                        jump.Play();
                        break;
                    case SoundType.musicPlay:
                        music.Play();
                        break;
                    case SoundType.musicStop:
                        music.Stop();
                        break;
                    case SoundType.storyPlay:
                        story.Play();
                        break;
                    case SoundType.storyStop:
                        story.Stop();
                        break;
                    case SoundType.battlePlayer:
                        battlePlayer.Play();
                        break;
                    case SoundType.battleEnemy:
                        battleEnemy.Play();
                        break;
                    case SoundType.diePlayer:
                        diePlayer.Play();
                        break;
                    case SoundType.dieEnemy:
                        dieEnemy.Play();
                        break;
                    case SoundType.star:
                        invulnerability.CreateInstance().Play();
                        break;
                    case SoundType.openDoor:
                        openDoor.Play();
                        break;
                    case SoundType.door:
                        door.Play();
                        break;
                    case SoundType.nextLevel:
                        nextLevel.Play();
                        break;
                    case SoundType.poison:
                        poison.CreateInstance().Play();
                        break;
                    case SoundType.dollar:
                        dollar.CreateInstance().Play();
                        break;
                    case SoundType.noSound:
                        break;
                
                }            
            }

            level.soundList.Clear();

        }

        public void StopMusic()
        {
            music.Stop();
            story.Stop();
        }

        //update service whene load the next level
        public void UpdateLevelService(ContentManager content)
        {
            level = content.ServiceProvider.GetService(typeof(Level)) as Level;
            float temp = rand.Next(4);
            music = content.Load<SoundEffect>("Sounds/songGame" + temp.ToString()).CreateInstance();
        }

    }
}
